package com.freecloud.javabasics.classload;

/**
 * 系统ClassLoader的类关系
 * @Author: maomao
 * @Date: 2019-11-08 10:52
 */
public class SystemClassLoader {

    public SystemClassLoader(){}

    public static void main(String[] args) {
        getClassLoader("java.lang.String");
        getClassLoader("sun.launcher.resources.launcher");
        getClassLoader("cn.hutool.core.util.StrUtil");
    }

    /**
     * 获取类加载器
     * 返回为null表示使用的BootStrap ClassLoader
     */
    public static void getClassLoader(String classPack){
        Class clazz;
        try {
            clazz = Class.forName(classPack);
            System.out.println(classPack + ":   " + clazz.getClassLoader());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取系统类加载关系
     */
    public static void getSystemClassLoader(){
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        System.out.println("获取系统类加载关系" + classLoader);

        while (classLoader != null){
            classLoader = classLoader.getParent();
            System.out.println("类加载关系parent:" + classLoader);
        }
    }
}
